package top.lucky.kafka.producer;

import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

/**
 * @author Jane
 * @date 2022/12/24, 20:12
 */
public class KafkaProducerSync {
	public static void main(String[] args) throws Exception {
		//配置
		Properties properties = new Properties();
		//连接集群
		properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"http://localhost:9193,http://localhost:9194,http://localhost:9195");
		properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
		properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
		
		//1.创建kafka生产者对象
		org.apache.kafka.clients.producer.KafkaProducer<String, String> kafkaProducer = new org.apache.kafka.clients.producer.KafkaProducer<>(properties);
		//2.发送数据
		for (int i = 0; i < 5; i++) {
			
			kafkaProducer.send(new ProducerRecord<>("first", "AAAA" + i)).get();
		}
		//3.关闭资源
		kafkaProducer.close();
	}
}
